package gov.pbc.nn.kjgl.gzjh.dao;

import java.util.List;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.core.entity.BaseEntity;
import gov.pbc.nn.kjgl.gzjh.entity.WorkContent;
import gov.pbc.nn.kjgl.gzjh.entity.WorkP;
import gov.pbc.nn.kjgl.gzjh.entity.WorkPlanBanch;
import gov.pbc.nn.kjgl.gzjh.entity.WorkPlanProcessUser;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/**
 * 描述：本类主要为了操作工作计划实体类的数据操作
 * 
 * @author jack.zhao
 * 
 */
@Repository
public class WorkDao extends AbstractHibernateDao {

	/**
	 * 工作计划页面的后台实体类加载方法
	 *
	 *@auther jack.zhao 
	 *@param page
	 *@param criteria
	 *@throws Exception
	 *
	 */
	public void loadWorks(Page<WorkP> page, Criteria criteria) throws Exception {
		DetachedCriteria dc = this.buildDetachedCriteria(criteria, WorkP.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("createUser", ContextHolder.getLoginUser().getUsername()));
		// dc.add(Restrictions.eq("deptId",
		// ContextHolder.getLoginUser().getDepts().get(0).getId()));
		dc.addOrder(Order.desc("createDate"));
		this.pagingQuery(page, dc);
	}

	/**
	 * 根据workId查找workBanch
	 *
	 *@auther jack.zhao 
	 *@param id
	 *@return
	 *
	 */
	@SuppressWarnings("unchecked")
	public List<WorkPlanBanch> getWorkPlanBanchById(String id) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkPlanBanch.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("workId", id));
		// dc.add(Restrictions.eq("banchUsername",ContextHolder.getLoginUser().getUsername()));
		return (List<WorkPlanBanch>) this.query(dc);
	}
//
//	@SuppressWarnings("unchecked")
//	public List<WorkPlanBanch> getWorkPlanBanchByDept(String id) {
//		DetachedCriteria dc = DetachedCriteria.forClass(WorkPlanBanch.class);
//		dc.add(Restrictions.eq("invalid", false));
//		dc.add(Restrictions.eq("workId", id));
//		// dc.addOrder(Order.desc("createDate"));
//		dc.add(Restrictions.eq("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId()));
//		List<WorkPlanBanch> lists = (List<WorkPlanBanch>) this.query(dc);
//		List<WorkPlanBanch> templist = new ArrayList<WorkPlanBanch>();
//		for (WorkPlanBanch b : lists) {
//			String id2 = b.getId();
//			List<WorkPlanProcessUser> workPlanProcessUsers = getWorkPlanProcessUserById(id2);
//			for (WorkPlanProcessUser wu : workPlanProcessUsers) {
//				if (wu.getProcessUsername().equals(ContextHolder.getLoginUser().getUsername())) {
//					templist.add(b);
//				}
//			}
//		}
//		return templist;
//	}

	/**
	 * 科长页面单独的后台实体加载方法，因为科长要加载banchUsername是自己的实体
	 *
	 *@auther jack.zhao 
	 *@param id
	 *@return
	 *
	 */
	@SuppressWarnings("unchecked")
	public List<WorkPlanBanch> getWorkPlanBanchByUsername(String id) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkPlanBanch.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("workId", id));
		dc.add(Restrictions.eq("banchUsername", ContextHolder.getLoginUser().getUsername()));
		return (List<WorkPlanBanch>) this.query(dc);
	}

	/**
	 * 
	 * 根据banchId查找workContent实体
	 *@auther jack.zhao 
	 *@param bancherId
	 *@return
	 *
	 */
	@SuppressWarnings("unchecked")
	public List<WorkContent> getWorkContentById(String bancherId) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkContent.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("userId", bancherId));
		dc.addOrder(Order.desc("createDate"));
		List<WorkContent> list = (List<WorkContent>) this.query(dc);
		return list;
	}

//	// 用户使用要根据两个参数才能确定一个workContet
//	public List<WorkContent> getWorkContentByBanchIdAndUserId(String banchId, String userId) {
//		DetachedCriteria dc = DetachedCriteria.forClass(WorkContent.class);
//		dc.add(Restrictions.eq("invalid", false));
//		dc.add(Restrictions.eq("userId", userId));
//		dc.add(Restrictions.eq("banchId", banchId));
//		dc.addOrder(Order.desc("createDate"));
//		List<WorkContent> list = (List<WorkContent>) this.query(dc);
//		return list;
//	}

	/**
	 * 根据banchId查找workPlanProcessUser集合，公用页面
	 *
	 *@auther jack.zhao 
	 *@param bancherId
	 *@return
	 *
	 */
	public List<WorkPlanProcessUser> getWorkPlanProcessUserById(String bancherId) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkPlanProcessUser.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("banchId", bancherId));
		// dc.add(Restrictions.eq("processUsername",ContextHolder.getLoginUser().getUsername()));
		List<WorkPlanProcessUser> lists = (List<WorkPlanProcessUser>) this.query(dc);
		return lists;
	}

	/**
	 * 根据banchId查找登陆人是自己的workPlanProcessUser集合，处理人计划页面
	 *
	 *@auther jack.zhao 
	 *@param bancherId
	 *@return
	 *
	 */
	public List<WorkPlanProcessUser> getWorkPlanProcessUserByIdByUsername(String bancherId) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkPlanProcessUser.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("banchId", bancherId));
		dc.add(Restrictions.eq("processUsername", ContextHolder.getLoginUser().getUsername()));
		List<WorkPlanProcessUser> lists = (List<WorkPlanProcessUser>) this.query(dc);
		return lists;
	}

	/**
	 * 公用根据实体状态保存更新实体的方法
	 *
	 *@auther jack.zhao 
	 *@param entity
	 *@throws Exception
	 *
	 */
	public void saveAndUpdateEntity(BaseEntity entity) throws Exception {
		EntityState state = EntityUtils.getState(entity);
		if (state.equals(EntityState.NEW)) {
			this.save(entity);
		} else if (state.equals(EntityState.MODIFIED)) {
			this.update(entity);
		} else if (state.equals(EntityState.DELETED)) {
			this.delete(entity);
		}
	}

//	public void deleteBancer(String id) {
//		this.delete(id, WorkPlanBanch.class);
//	}
}
